package sysu.el.array.type;

import sysu.el.array.ints.ArrInt;

/**
 * OK, this array should be as simple as possible.
 * @author Edwin
 *
 */
public class ArrType<T> implements java.util.RandomAccess{
	public int counter;
	public T list[];
	
	public T[] elements(){
		return list;
	}
	
	/**
	 * Private, just for wrap()
	 */
	private ArrType(){
		
	}
	
	public static<T> ArrType<T> wrap(int counter, T list[]){
		ArrType<T> arr = new ArrType<T>();
		arr.counter = counter;
		arr.list = list;
		return arr;
	}
	
	public ArrType(int num){
//		this.list = T.build();
		this.init();
	}
	
	public void init(){
		counter = 0;
	}
	
	public void reset(){
		counter = 0;
	}
	
	public void add(T elem){
		list[counter ++] = elem;
	}
	
	public T[] toArray(){
		T[] new_list = (T[])new Object[this.counter];
		System.arraycopy(this.list, 0, new_list, 0, counter);
		return new_list;
	}
	
	public void reduceSize(){
		T[] new_list = (T[])(new Object[this.counter]);
		this.list = this.toArray();
	}
	
//	public ArrInt createReducedVersion(){
//		return ArrInt.buildReducedVersion(this);
//	}
	
	public static<T> ArrType<T> buildReducedVersion(ArrType<T> prev){
		int len = prev.counter;
		ArrType<T> cur = new ArrType<T>(len);
		System.arraycopy(prev, 0, cur.list, 0, len);
		cur.counter = len;
		return cur;
	}
	
	public void swap(int a, int b){
		int tmp = a;
		a = b;
		b = tmp;
	}
	
	public static ArrInt NULL = new ArrInt(0);

}


